import { defineStore } from "pinia";
import { getMenu, getRouteList } from "@/api/api";
import router from "@/router";
const modules = import.meta.glob("../views/*/*.vue");
// 侧边栏菜单以及路由获取

export const useMenuStore = defineStore("menu", {
  state: () => {
    return {
      isFold: false,
      menuList: [],
      routerList: [],
    };
  },
  actions: {
    toggleMenuFold() {
      this.isFold = !this.isFold;
    },
    async getMenuList() {
      this.menuList = await getMenu();
    },
    async getRouteList() {
      let res = await getRouteList();

        res.forEach((i) => {
          const pathSegments = i.path.split("/");
          const componentName = pathSegments[pathSegments.length - 1];
          const componentPath = `../views/${componentName}/index.vue`;

          const route = {
            path: i.path,
            name: i.name,
            component: () => import(componentPath),
            children: i.children || [],
          };
          
          router.addRoute("home", route);
        });
      }
      // console.log(router.getRoutes(), "s");
  },
  persist: true,
});
